package com.zysc.processflow.mapper;

import com.zysc.processflow.entity.Handlerecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zysc.processflow.pojo.HandleRecordPO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 处置流程记录表 Mapper 接口
 * </p>
 *
 * @author 正元智慧城市_
 * @since 2022-11-18
 */
@Repository
public interface HandlerecordMapper extends BaseMapper<Handlerecord> {

    @Select("select a.*,b.time from handlerecord a LEFT JOIN alarms b on a.alarmid = b.id where a.id in (\n" +
            "\tselect record.id from handlerecord record LEFT JOIN (select dt.* from handlerecorddetail dt WHERE dt.type = '处置任务' ) detail on record.id = detail.recordid where record.handlestatus in ('0','3') \n" +
            "\tGROUP BY record.id HAVING count(detail.id) =0 )")
    List<HandleRecordPO> getNotFinishedAlarmRecord();

    @Select("select a.*,b.time from handlerecord a LEFT JOIN alarms b on a.alarmid = b.id where a.id in (\n" +
            "\tselect record.id from handlerecord record LEFT JOIN (select dt.* from handlerecorddetail dt WHERE dt.type = '处置任务' ) detail on record.id = detail.recordid where record.handlestatus in ('0','3') \n" +
            "\tGROUP BY record.id HAVING count(detail.id) =0\n" +
            ") and b.time > #{time} ")//0为处置未完成,3缺处置人的流程
    List<HandleRecordPO> getNotFinishedAlarmRecordAfterTime(@Param("time")long time);

    @Select("select a.*,b.time from handlerecord a LEFT JOIN alarms b on a.alarmid = b.id where a.id in (\n" +
            "\tselect record.id from handlerecord record LEFT JOIN (select dt.* from handlerecorddetail dt WHERE dt.type = '处置任务' ) detail on record.id = detail.recordid where record.handlestatus in ('0','3') \n" +
            "\tGROUP BY record.id HAVING count(detail.id) =0 ) and b.endtime isnull ")
    List<HandleRecordPO> getNotFinishedAlarmRecordOnlyHasNotEnded();
}
